package com.itany.corejava.day12;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestEvent implements ActionListener {

    private JFrame frame;
    private JButton eastButton;
    private JButton southButton;
    private JButton westButton;
    private JButton northButton;
    private JTextArea area;


    public TestEvent() {
        frame=new JFrame();
        eastButton=new JButton("东");
        southButton=new JButton("南");
        westButton=new JButton("西");
        northButton=new JButton("北");
        area=new JTextArea(20,40);
    }

    private void init(){
        frame.add(eastButton, BorderLayout.EAST);
        frame.add(southButton,BorderLayout.SOUTH);
        frame.add(westButton,BorderLayout.WEST);
        frame.add(northButton,BorderLayout.NORTH);
        frame.add(area,BorderLayout.CENTER);
    }

    private void setStyle(){
        Font font=new Font("宋体",Font.BOLD,26);
        area.setFont(font);

        area.setForeground(Color.RED);
        area.setBackground(new Color(178,178,178));
    }

    private void addEventHandler(){
        eastButton.addActionListener(this);
        southButton.addActionListener(this);
        westButton.addActionListener(this);
        northButton.addActionListener(this);
    }

    public void show(){
        init();
        setStyle();
        addEventHandler();
        frame.pack();;
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    public static void main(String[] args) {
        new TestEvent().show();
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("TestEvent.actionPerformed");
        String cmd=e.getActionCommand();
        System.out.println(cmd);
        System.out.println(e.getSource());
//        area.setText(area.getText()+cmd);
        area.append(cmd+"被点击\n");
    }
}
